package com.ttctracker.map;

import com.ttctracker.R;
import com.ttctracker.classes.Prediction;
import com.ttctracker.classes.StopItem;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class StopPredictionArrayAdapter extends ArrayAdapter<StopItem> {
	private String[] dirTitles;
	private StopItem[] stopItems;
	public StopPredictionArrayAdapter(Context context, int textViewResourceId,
			StopItem[] objects,String[] dirTitles) {
		super(context, textViewResourceId, objects);

		this.stopItems=objects;
		this.dirTitles=dirTitles;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.row_stop_predictions, null);
		}
		
		StopItem item=stopItems[position];
		String dir=dirTitles[position];
		TextView routeNum=(TextView)v.findViewById(R.id.row_stop_predictions_route);
		TextView routeDir=(TextView)v.findViewById(R.id.row_stop_predictions_dir);
		TextView prediction1=(TextView)v.findViewById(R.id.row_stop_predictions1);
		TextView prediction2=(TextView)v.findViewById(R.id.row_stop_predictions2);
		TextView prediction3=(TextView)v.findViewById(R.id.row_stop_predictions3);
		
		routeNum.setText(item.get_routeTag());
		routeDir.setText(dir);
		Prediction prediction=item.get_prediction();
		int size=prediction.size();
		prediction1.setVisibility(View.VISIBLE);
		prediction2.setVisibility(View.VISIBLE);
		prediction3.setVisibility(View.VISIBLE);
		if (size>2) {
			
			prediction1.setText(Integer.toString(prediction.get(0))+" min");
			prediction2.setText(Integer.toString(prediction.get(1))+" min");
			prediction3.setText(Integer.toString(prediction.get(2))+" min");
		}else if (size>1) {
			prediction3.setVisibility(View.GONE);
			prediction1.setText(Integer.toString(prediction.get(0))+" min");
			prediction2.setText(Integer.toString(prediction.get(1))+" min");
			
			//prediction3.setText(Integer.toString(prediction.get(2))+" min");
		}else {
			int firstPrediction=prediction.get(0);
			prediction2.setVisibility(View.GONE);
			prediction3.setVisibility(View.GONE);
			if (firstPrediction<0) {
				
				prediction1.setText("Not Available");
			}else {
				prediction1.setText(firstPrediction+ " min");
			}
		}
		
		return v;
	}
	
}
